/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ace.states;

import ace.game.Sprite;
import ace.game.entity.player.Profile;
import ace.system.BitmapFont;
import ace.system.Model;
import ace.system.StateManager;
import ace.system.Utilities;
import ace.system.sound.SFXPlayer;
import ace.system.sound.SimplePlayer;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author TimotiusNC
 */
public class StateMenu extends GameState {
	protected static final long ANIMATION_DELAY = 100;
	
	public StateMenu(Vector param){
		super(GameState.MENU, StateManager.instance().getBackgroundImage());

		m_Param = param;
        m_Profile = (Profile)m_Param.elementAt(0);
		initComp();
	}

	private void initComp(){
		//Initialize
		m_Draw		= true;
		m_Animate	= true;
		m_Index		= 0;
		m_CircleX2	= m_05Width;
		m_CircleY	= m_15Height+m_OffsetY;

		//Initialize animation
		m_AnimationTime	= 0;
		m_Entering		= false;
		m_Exiting		= false;

		try { //Initialize images
			//m_TitleArrowImage	= Image.createImage(Utilities.INTERFACE_FOLDER + "/title_arrow.png");
			m_LiteImage			= Image.createImage(Utilities.INTERFACE_FOLDER + "/lite.png");
			m_Arrow				= new Sprite(Utilities.INTERFACE_FOLDER + "/arrow.png", 7, 98, 3, 1);

            m_ProfileNameStringImage    = BitmapFont.getFont(BitmapFont.TITLE).renderTransparentText(m_Profile.getProfileName(), 0x000000);
            m_AccomplishmentStringImage = BitmapFont.getFont(BitmapFont.BITSUMISHI_BLUE).renderTransparentText("Finished", 0x000000);
            m_MissionStringImage        = BitmapFont.getFont(BitmapFont.BITSUMISHI_BLUE).renderTransparentText("Mission", 0x000000);
            m_AvgRankStringImage        = BitmapFont.getFont(BitmapFont.BITSUMISHI_BLUE).renderTransparentText("Rank", 0x000000);
			m_MenuStringImg = new Image[m_Menu.length];
			for(int i=0; i<m_Menu.length; ++i){
				m_MenuStringImg[i] = BitmapFont.getFont(BitmapFont.TITLE_BIG).renderTransparentText(m_Menu[i], 0x00000000);
			}
		} catch (IOException ex) {}

		//Draws
		m_Draw = true;
	}

	public void onEnter() {
		StateManager.instance().getSimplePlayer().setSong(Utilities.BGM_FOLDER + "/title.mid", "audio/mid", SimplePlayer.LOOP_INF);
		StateManager.instance().getSimplePlayer().startPlayer();

		m_Draw = true;
        m_MoneyStringImage      = BitmapFont.getFont(BitmapFont.BITSUMISHI_BLUE).renderTransparentText(m_Profile.getMoney() + " DP", 0x000000);
        final int accValue      = m_Profile.getAccomplishment();
        final int misValue      = m_Profile.getLevel();
        final float rnkValue    = (float)m_Profile.getTotalMissionScore() / ((float)m_Profile.getLevel() - 1);

        m_AccValueStringImage   = BitmapFont.getFont(BitmapFont.BITSUMISHI_BLUE).renderTransparentText(accValue + "%", 0x000000);
        m_MisValueStringImage   = BitmapFont.getFont(BitmapFont.BITSUMISHI_BLUE).renderTransparentText(misValue + "", 0x000000);
        try {
            if (rnkValue > 4.5) {
                m_AvgValueImage     = Image.createImage(Utilities.INTERFACE_FOLDER + "/rank_s.png");
            } else if (rnkValue > 3.5) {
                m_AvgValueImage     = Image.createImage(Utilities.INTERFACE_FOLDER + "/rank_a.png");
            } else if (rnkValue > 2.5) {
                m_AvgValueImage     = Image.createImage(Utilities.INTERFACE_FOLDER + "/rank_b.png");
            } else if (rnkValue > 1.5) {
                m_AvgValueImage     = Image.createImage(Utilities.INTERFACE_FOLDER + "/rank_c.png");
            } else if (rnkValue > 0.5) {
                m_AvgValueImage     = Image.createImage(Utilities.INTERFACE_FOLDER + "/rank_d.png");
            } else {
                m_AvgValueImage     = Image.createImage(Utilities.INTERFACE_FOLDER + "/rank_e.png");
            }
        } catch (IOException iEx) {

        }
        // save profile
        Model.writeModel(m_Profile.getSaveIndex(), m_Profile);

		//Animate?
		if (m_Animate) m_Entering = true;
	}

	public void keyPressed(int key) {
		super.keyPressed(key);

		//Is right pressed?
		if (key == -7 || key == 35) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CANCEL);

			//Exiting animation
			m_Exiting = true;
		}
	}

	public void update(long time, int keys) {
		super.update(time, keys);

		//Update thingy
		if( ((keys & GameCanvas.FIRE_PRESSED) != 0) ){
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_SELECT);
			switch(m_Index){
				case 0: //Mission clicked
					Vector temp = new Vector();
                    temp.addElement(new Integer(GameState.GAME));
					temp.addElement(new Integer(m_Profile.getLevel()));
                    temp.addElement(m_Profile);
					StateManager.instance().goTo(GameState.LOADING, temp, false);
					break;
				case 1: //Garage clicked
					StateManager.instance().goTo(GameState.GARAGE_MENU, m_Param, false);
					break;
				case 2: //Exit clicked
					m_Active = false;
					break;
			}
		}

		//Animation thingy
		if(m_Entering){
			//Skip if first time
			if (m_AnimationTime == 0) m_AnimationTime = 1;
			else {
				//Increase
				m_AnimationTime += time;
				m_Draw			= true;

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_CircleY			= (m_15Height+m_OffsetY) - (int)(StateManager.instance().getHeight() * Comparison);

				//Check time and fire
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) {
					m_Entering		= false;
					m_AnimationTime = 0;

					//Finalize circle
					m_CircleY	= m_05Height+m_OffsetY;
				}
			}
		} else if (m_Exiting) {
			//Skip if first time
			if (m_AnimationTime == 0) m_AnimationTime = 1;
			else {
				//Increase
				m_AnimationTime += time;
				m_Draw			= true;

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_CircleY			= (m_05Height+m_OffsetY) - (int)(StateManager.instance().getHeight() * Comparison);

				//Check time and fire
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) {
					m_Exiting		= false;
					m_AnimationTime = 0;

					//Finalize circle
					m_CircleY	= -m_05Height+m_OffsetY;

					//Back to previous state
					//m_Active = false;
                    StateManager.instance().goTo(GameState.FRONT, null, false);
				}
			}
		} else if (m_ToLeft) {
			//Skip if first time
			if (m_AnimationTime == 0) m_AnimationTime = 1;
			else {
				//Increase
				m_AnimationTime += time;
				m_Draw			= true;

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_CircleX2			= m_15Width - (int)(StateManager.instance().getWidth() * Comparison);
				m_CircleX			= m_05Width - (int)(StateManager.instance().getWidth() * Comparison);

				//Check time and fire
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) {
					m_ToLeft		= false;
					m_AnimationTime = 0;

					//Finalize circle
					m_CircleX2	= m_05Width;
					m_CircleX	= -m_05Width;
				}
			}
		} else if(m_ToRight) {
			if (m_AnimationTime == 0) m_AnimationTime = 1;
			else {
				//Increase
				m_AnimationTime += time;
				m_Draw			= true;

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_CircleX2			= -m_05Width + (int)(StateManager.instance().getWidth() * Comparison);
				m_CircleX			= m_05Width + (int)(StateManager.instance().getWidth() * Comparison);

				//Check time and fire
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) {
					m_ToRight		= false;
					m_AnimationTime = 0;

					//Finalize circle
					m_CircleX2	= m_05Width;
					m_CircleX	= m_15Width;
				}
			}
		} else if ((keys & GameCanvas.LEFT_PRESSED) != 0 ) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);

			//Prev
			m_Index--;
			if(m_Index < 0)	m_Index = m_Menu.length-1;

			m_ToRight	= true;
			m_CircleX	= m_05Width;
			m_CircleX2	= -m_05Width;

			//Animate
			m_Time = ANIMATION_DELAY;
			m_Arrow.playAnimation(1, false);

			m_Draw = true;
		} else if ((keys & GameCanvas.RIGHT_PRESSED) != 0) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);
			
			//Next
			m_Index++;
			if(m_Index >= m_Menu.length)	m_Index = 0;
			
			m_ToLeft	= true;
			m_CircleX	= m_05Width;
			m_CircleX2	= m_15Width;

			//Animate
			m_Time = ANIMATION_DELAY;
			m_Arrow.playAnimation(2, false);
			
			m_Draw = true;
		}

		//Reset arrow
		if (m_Time > 0) m_Time -= time;
		else {
			m_Time = 0;
			m_Arrow.playAnimation(0, false);
		}
	}

	public void draw(Graphics g) {
		if(!m_Draw) return;
		
		super.draw(g);

		//g.drawImage(m_TitleArrowImage, -5, -15, Graphics.LEFT | Graphics.TOP);
		//g.drawImage(m_LiteImage, StateManager.instance().getWidth() - 55, 45, Graphics.LEFT | Graphics.TOP);

		if(m_ToLeft){
			//g.drawImage(m_Circle, m_CircleX2, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
            // hide most of the circle until nearby destination
            if (m_CircleX2+m_OffsetX < 200)
                g.drawImage(m_MenuStringImg[m_Index], m_CircleX2+m_OffsetX, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);

			//g.drawImage(m_Circle, m_CircleX, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
			g.drawImage(m_MenuStringImg[m_Index-1 < 0 ? m_Menu.length-1 : m_Index-1], m_CircleX+m_OffsetX, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
		}else if(m_ToRight){
			//g.drawImage(m_Circle, m_CircleX2, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
			g.drawImage(m_MenuStringImg[m_Index], m_CircleX2+m_OffsetX, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);

			//g.drawImage(m_Circle, m_CircleX, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
            if (m_CircleX+m_OffsetX < 200)
                g.drawImage(m_MenuStringImg[m_Index+1 >= m_Menu.length ? 0 : m_Index+1], m_CircleX+m_OffsetX, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
		}

		if(!m_ToLeft && !m_ToRight){
			//g.drawImage(m_Circle, m_CircleX2, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
			g.drawImage(m_MenuStringImg[m_Index], m_CircleX2+m_OffsetX, m_CircleY, Graphics.HCENTER | Graphics.VCENTER);
		}

        g.drawImage(m_MoneyStringImage, StateManager.instance().getWidth() - 5, 5, Graphics.TOP | Graphics.RIGHT);
        g.drawImage(m_ProfileNameStringImage, 210, 70, Graphics.TOP | Graphics.LEFT);
        g.drawImage(m_MissionStringImage, 210, 95, Graphics.TOP | Graphics.LEFT);
        g.drawImage(m_AccomplishmentStringImage, 210, 110, Graphics.TOP | Graphics.LEFT);
        g.drawImage(m_AvgRankStringImage, 210, 125, Graphics.TOP | Graphics.LEFT);
        g.drawImage(m_MisValueStringImage, StateManager.instance().getWidth() - 5, 95, Graphics.TOP | Graphics.RIGHT);
        g.drawImage(m_AccValueStringImage, StateManager.instance().getWidth() - 5, 110, Graphics.TOP | Graphics.RIGHT);
        g.drawImage(m_AvgValueImage, StateManager.instance().getWidth() - 5, 125, Graphics.TOP | Graphics.RIGHT);

		if(!m_Entering && !m_Exiting)
			m_Arrow.draw(g);
		
		//Stop drawing
		m_Draw = false;
	}

	//Animation Thingy
	protected final long ANIMATE_TIME = 500;
	protected final int	m_15Width	= (int) (1.5*StateManager.instance().getWidth());
	protected final int	m_05Width	= (int) (StateManager.instance().getWidth()/2);
	protected final int	m_15Height	= (int) (1.5*StateManager.instance().getHeight());
	protected final int	m_05Height	= (int) (StateManager.instance().getHeight()/2);
	protected final int	m_OffsetX	= -67;
	protected final int	m_OffsetY	= -1;

	//Data
	protected boolean	m_Draw;
	protected int		m_Index;
	protected String[]	m_Menu = {"Mission", "Garage", "Exit"};
	protected Vector	m_Param;
    protected Profile   m_Profile;

	//Images
	protected Image		m_LiteImage;
	protected Image[]	m_MenuStringImg;
    protected Image     m_MoneyStringImage;
    protected Image     m_ProfileNameStringImage;
    protected Image     m_AccomplishmentStringImage, m_AccValueStringImage;
    protected Image     m_MissionStringImage, m_MisValueStringImage;
    protected Image     m_AvgRankStringImage, m_AvgValueImage;

	//Animation
	protected boolean	m_Animate;
	protected boolean	m_Entering, m_Exiting;
	protected boolean	m_ToRight, m_ToLeft;
	protected int		m_AnimationTime;
	protected int		m_CircleX, m_CircleX2, m_CircleY;
	protected Sprite	m_Arrow;
	protected long		m_Time;
}
